package array.nowcoder.easy;

/**
 * @author bruin_du
 * @description 微信红包里出现次数超过一半的数值
 * https://www.nowcoder.com/practice/fbcf95ed620f42a88be24eb2cd57ec54?
 * tpId=49&&tqId=29311&rp=1&ru=/activity/oj&qru=/ta/2016test/question-ranking
 * @date 2022/5/20 15:54
 **/
public class WeChatGift {
    public int getValue(int[] gifts, int n) {
        int num = gifts[0],count = 0;
        for (int gift : gifts) {
            if(gift == num) count++;
            else count--;

            if(count == 0){
                num = gift;
                count = 1;
            }
        }
        int times = 0;
        for (int gift : gifts) {
            if(num == gift)
                times++;
        }
        return times > n / 2 ? num : 0;
    }
}
